<template>
	<view class="wrap-item flex align-center just-between" v-if="item">
		<view class="title-bg flex">
			<view class="must" v-if="item.require">
				*
			</view>

			<view class="title">
				{{ item.title }}
			</view>
		</view>

		<view class="wrap-right flex align-center">
			<input
				class="input text-right"
				:placeholder="item.place"
				placeholder-class="place"
				:type="item.mode === undefined ? 'text' : item.mode"
				maxlength="-1"
				v-model="modelValue"
				:disabled="item.disabled === undefined ? false : item.disabled"
			/>
			
			<view 
				class="unit"
				v-if="item.hasOwnProperty('unit')"
			>
				{{ item.unit }}
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			/**
			 * 输入框的值
			 */
			value: {
				type: String,
				default: "",
			},
			/**
			 * 数据
			 */
			item: {
				type: Object,
				default: null,
			},
		},
		watch: {
			"modelValue": {
				handler() {
					if (this.maxLength > 0) {
						setTimeout(() => {
							this.modelValue = this.modelValue.substr(0, this.maxLength);
						}, 0);
					}
				},
				immediate: true,
			},
		},
		computed: {
			/**
			 * 最大长度
			 */
			maxLength() {
				var number = -1;
				if (this.item && this.item.hasOwnProperty("maxlength")) {
					number = this.item.maxlength;
				}
				return number;
			},
			/**
			 * 输入的value
			 */
			modelValue: {
				set(val) {
					this.$emit("input", val);
				},
				get() {
					return this.value
				}
			},
		},
		data() {
			return {
			}
		},
		methods: {
		}
	};
</script>

<style>
	@import "../css/form.css";
	
	.wrap-item {
		gap: 20rpx;
	}
	
	.input {
		display: block;
	}
	
	.wrap-right {
		flex: 1;
		gap: 15rpx;
		overflow: hidden;
	}
</style>
